<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="订单">
              <j-select-order placeholder="请选择订单" v-decorator="['orderId', validatorRules.orderId]" :trigger-change="true"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="程序员">
              <j-select-programmer placeholder="请选择程序员" v-decorator="['programmerId', validatorRules.programmerId]" :trigger-change="true"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="结算金额">
              <a-input-number style="width: 100%" placeholder="请输入结算金额" v-decorator="['billAmount', validatorRules.billAmount ]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="结算类型">
              <j-dict-select-tag type="list" v-decorator="['billType', validatorRules.billType]" :trigger-change="true" dictCode="bill_type" placeholder="请选择结算类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="支付方式">
              <j-dict-select-tag type="list" v-decorator="['paymentMethod', validatorRules.paymentMethod]" :trigger-change="true" dictCode="payment_method" placeholder="请选择支付方式"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="结算状态">
              <j-dict-select-tag type="list" v-decorator="['billStatus', validatorRules.billStatus]" :trigger-change="true" dictCode="bill_status" placeholder="请选择结算状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="结算时间">
              <j-date placeholder="请选择结算时间" v-decorator="['billTime', validatorRules.billTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item
              :labelCol="{span: 3}"
              :wrapperCol="{span: 20}"
              label="结算备注">
              <a-textarea placeholder="请输入结算备注" v-decorator="['billRemark', {}]" :rows="4" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JSelectOrder from './components/JSelectOrder'
  import JSelectProgrammer from './components/JSelectProgrammer'

  export default {
    name: "BProgrammerBillInfoModal",
    components: {
      JSelectOrder,
      JSelectProgrammer
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          orderId: {rules: [{ required: true, message: '请选择订单!' }]},
          programmerId: {rules: [{ required: true, message: '请选择程序员!' }]},
          billAmount: {rules: [{ required: true, message: '请输入结算金额!' }]},
          billType: {rules: [{ required: true, message: '请选择结算类型!' }]},
          paymentMethod: {rules: [{ required: true, message: '请选择支付方式!' }]},
          billStatus: {rules: [{ required: true, message: '请选择结算状态!' }]},
          billTime: {rules: [{ required: true, message: '请选择结算时间!' }]},
        },
        url: {
          add: "/order/bProgrammerBillInfo/add",
          edit: "/order/bProgrammerBillInfo/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'orderId','programmerId','billAmount','billType','paymentMethod','billStatus','billRemark'))
          if(this.model.billTime){
            this.form.setFieldsValue({billTime:moment(this.model.billTime)})
          }
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.billTime = formData.billTime?formData.billTime.format('YYYY-MM-DD'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>
</style> 